/*
 * FileUtilsTest.java
 *
 * Created on 15 April 2007, 10:34
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.cardcopier.file;

import java.io.File;
import java.io.IOException;
import javax.print.attribute.standard.Destination;
import junit.framework.TestCase;

/**
 *
 * @author Mark
 */
public class FileUtilsTest extends TestCase {
    
    private static final File SOURCE_DIR = new File("test-data", "source");
    private static final File DESTINATION_DIR = new File("build", "FileUtilsTest");
    
    /** Creates a new instance of FileUtilsTest */
    public FileUtilsTest() {
    }
    
    public void setUp() {
        if (DESTINATION_DIR.exists()) {
            File[] contents = DESTINATION_DIR.listFiles();
            for (File file : contents) {
                file.delete();
            }
            DESTINATION_DIR.delete();
        }        
    }

    public void testFileCopyWhenDestinationDirAlreadyExists() throws IOException {
        String filename = "file-1.cr2";
        File source = new File(SOURCE_DIR, filename);
        File destination = new File(DESTINATION_DIR, filename);
        DESTINATION_DIR.mkdir();
        new FileUtils().copyFile(source, destination);
        assertTrue(destination.exists());
    }

    public void testFileCopyWhenDestinationDirDoesNotAlreadyExist() throws IOException {
        String filename = "file-1.cr2";
        File source = new File(SOURCE_DIR, filename);
        File destination = new File(DESTINATION_DIR, filename);
        assertFalse(destination.exists());        
        new FileUtils().copyFile(source, destination);
        assertTrue(destination.exists());
    }
    
    public void testDirectoryDeleteWhenDirDoesNotExist() throws IOException {
        assertFalse(DESTINATION_DIR.exists());
        new FileUtils().deleteDirAndContents(DESTINATION_DIR);
        assertFalse(DESTINATION_DIR.exists());
    }

    public void testDirectoryDeleteWhenDirIsNotEmpty() throws IOException {
        DESTINATION_DIR.mkdir();
        new File(DESTINATION_DIR, "foo").createNewFile();
        new File(DESTINATION_DIR, "bar").createNewFile();
        new File(DESTINATION_DIR, "baz").mkdir();
        new FileUtils().deleteDirAndContents(DESTINATION_DIR);
        assertFalse(DESTINATION_DIR.exists());
    }
    
    public void testCanFormatFileSizeAsKiloBytes() {
        String expectedFormattedSize = "5,859 KB";
        String actualFormattedSize = new FileUtils().formatFileSize(6000000);
        assertEquals(expectedFormattedSize, actualFormattedSize);
    }
    
    public void testCanGetSpaceAvailable() throws IOException {
        // Not much of test
        long numBytes = new FileUtils().spaceAvailableInBytes(DESTINATION_DIR);
        assertTrue(numBytes > 0);
    }
    
    public void testCanGetSpaceUsed() {
        // Not much of test
        long numBytes = new FileUtils().spaceUsedInBytes(SOURCE_DIR);
        assertTrue(numBytes > 0);
    }
}
